package com.youlai.boot.system.controller;

import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.model.Option;
import com.youlai.boot.system.service.SpotCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.SpotCategoryForm;
import com.youlai.boot.system.model.query.SpotCategoryQuery;
import com.youlai.boot.system.model.vo.SpotCategoryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 预售票分类前端控制层
 *
 * @author lwb
 * @since 2025-06-29 16:31
 */
@Tag(name = "预售票分类接口")
@RestController
@RequestMapping("/api/v1/spotCategorys")
@RequiredArgsConstructor
public class SpotCategoryController  {

    private final SpotCategoryService spotCategoryService;

    //@Operation(summary = "预售票分类分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:spotCategory:query')")
    public PageResult<SpotCategoryVO> getSpotCategoryPage(SpotCategoryQuery queryParams ) {
        IPage<SpotCategoryVO> result = spotCategoryService.getSpotCategoryPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增预售票分类")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:spotCategory:add')")
    @RepeatSubmit(expire = 3)
    @Log(value = "新增预售票分类", module = LogModuleEnum.OTHER)
    public Result<Void> saveSpotCategory(@RequestBody @Valid SpotCategoryForm formData ) {
        boolean result = spotCategoryService.saveSpotCategory(formData);
        return Result.judge(result);
    }

    //@Operation(summary = "获取预售票分类表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:spotCategory:edit')")
    public Result<SpotCategoryForm> getSpotCategoryForm(
        @Parameter(description = "预售票分类ID") @PathVariable Long id
    ) {
        SpotCategoryForm formData = spotCategoryService.getSpotCategoryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改预售票分类")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:spotCategory:edit')")
    @RepeatSubmit(expire = 3)
    @Log(value = "修改预售票分类", module = LogModuleEnum.OTHER)
    public Result<Void> updateSpotCategory(
            @Parameter(description = "预售票分类ID") @PathVariable Long id,
            @RequestBody @Validated SpotCategoryForm formData
    ) {
        boolean result = spotCategoryService.updateSpotCategory(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除预售票分类")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:spotCategory:delete')")
    @Log(value = "删除预售票分类", module = LogModuleEnum.OTHER)
    public Result<Void> deleteSpotCategorys(
        @Parameter(description = "预售票分类ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = spotCategoryService.deleteSpotCategorys(ids);
        return Result.judge(result);
    }

    //@Operation(summary = "预售票分类下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> getSpotCategorys() {
        List<Option<Long>> list = spotCategoryService.listSpotCategorysOptions();
        return Result.success(list);
    }
}
